<template>
  <div style="padding-top: 46px">
    <template v-if="user" >
      <van-cell title="昵称" is-link to="/my/update/edit" :value="user.nickName"  @click="toEdit('nickName', '昵称', user.nickName)"/>
      <van-cell title="账号" :value="user.userAccount"/>
      <van-cell title="头像" is-link to="/my/update/edit">
        <img style="height: 48px" :src="user.avatarUrl"/>
      </van-cell>
      <van-cell title="性别" is-link :value="user.gender" @click="toEdit('gender', '性别', user.gender)"/>
      <van-cell title="电话" is-link to="/my/update/edit" :value="user.phone" @click="toEdit('phone', '电话', user.phone)"/>
      <van-cell title="邮箱" is-link to="/my/update/edit" :value="user.email" @click="toEdit('email', '邮箱', user.email)"/>
      <van-cell title="注册时间" :value="user.createTime"/>
    </template>
  </div>

  <div style="display: flex; justify-content: center; align-items: center; height: 30vh;">
    <van-button type="danger" @click="removeAC" >注销账号</van-button>
  </div>
</template>

<script setup lang="ts">
import {useRouter} from "vue-router";
import {onMounted, ref} from "vue";
import myAxios from "../plugins/myAxios";
import {getCurrentUser} from "../services/user";

// const user = {
//   id: 1,
//   username: '鱼皮',
//   userAccount: 'dogYupi',
//   avatarUrl: 'https://636f-codenav-8grj8px727565176-1256524210.tcb.qcloud.la/img/logo.png',
//   gender: '男',
//   phone: '123112312',
//   email: '12345@qq.com',
//   planetCode: '1234',
//   createTime: new Date(),
// }

const user = ref();

onMounted(async () => {
  const res = await getCurrentUser();
  // @ts-ignore
  if (res.code === 200) {
    user.value = res.data
  }
})
const router = useRouter();

const toEdit = (editKey: string, editName: string, currentValue: string) => {
  router.push({
    path: '/my/update/edit',
    query: {
      editKey,
      editName,
      currentValue,
    }
  })
}

const removeAC = async () => {
  try {
    const res = await myAxios.get('/delete',{
      withCredentials: true,
      params: {
        id: user.value.id
      }
    });
    // @ts-ignore
    if (res?.code === 200) {
      console.log("注销成功");
      await router.replace("/team");
    } else {
      console.log("失败");
    }
  } catch (error) {
    console.error("请求失败", error);
  }
};

</script>

<style scoped>

</style>